package com.lb.shopguide.ui.fragment.boss;

import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.entity.BossActivityBean;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.BossMainActivity;
import com.lb.shopguide.ui.view.ClearTitleBar;
import com.lb.shopguide.ui.view.LbActivityItem;
import com.lb.shopguide.util.json.JsonUtil;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by kang on 2017/12/25.
 */

public class FragmentBossActivity extends BaseCommonFragment {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ctb)
    ClearTitleBar ctb;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmptyView;
    @BindView(R.id.layout_activity_hot)
    LinearLayout layoutActivityHot;
    @BindView(R.id.layout_activity_mine)
    LinearLayout layoutActivityMine;
    @BindView(R.id.layout_activity_history)
    LinearLayout layoutActivityHistory;
    @BindView(R.id.layout_whole_his)
    LinearLayout layoutWholeHistory;
    @BindView(R.id.layout_whole_mine)
    LinearLayout layoutWholeMine;
    @BindView(R.id.layout_whole_hot)
    LinearLayout layoutWholeHot;

    private MaterialHeader mMaterialHeader;

    private ArrayList<BossActivityBean> mActivityHotList;
    private ArrayList<BossActivityBean> mActivityMineList;
    private ArrayList<BossActivityBean> mActivityHistoryList;



    public static FragmentBossActivity newInstance() {
        return new FragmentBossActivity();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_boss_activity;
    }

    @Override
    public void initData() {
        mActivityHotList = new ArrayList<>();
        mActivityMineList = new ArrayList<>();
        mActivityHistoryList = new ArrayList<>();
        sendRequestGetActivityList();
    }

    @Override
    public void initView() {
        mMaterialHeader = (MaterialHeader) refreshLayout.getRefreshHeader();

        ctb.setTvTitle("活动中心");
        ctb.setLeftImageSrc(R.drawable.iv_back);

        initListener();
    }

    @Override
    public void finishRefresh() {
        refreshLayout.finishRefresh();
    }

    @Override
    public void sessionOutOfDate() {
        ((BossMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
        finishRefresh();
    }

    private void initListener() {
        ctb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
        refreshLayout.setOnRefreshListener(new OnMultiPurposeListener() {
            @Override
            public void onHeaderPulling(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {

            }

            @Override
            public void onHeaderReleased(RefreshHeader header, int headerHeight, int extendHeight) {

            }

            @Override
            public void onHeaderReleasing(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {

            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int extendHeight) {

            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {

            }

            @Override
            public void onFooterPulling(RefreshFooter footer, float percent, int offset, int footerHeight, int extendHeight) {

            }

            @Override
            public void onFooterReleased(RefreshFooter footer, int footerHeight, int extendHeight) {

            }

            @Override
            public void onFooterReleasing(RefreshFooter footer, float percent, int offset, int footerHeight, int extendHeight) {

            }

            @Override
            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int extendHeight) {

            }

            @Override
            public void onFooterFinish(RefreshFooter footer, boolean success) {

            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                sendRequestGetActivityList();
            }

            @Override
            public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {

            }
        });
    }

    @Override
    public String getPageTag() {
        return FragmentBossActivity.class.getName();
    }

    private void sendRequestGetActivityList() {
        mActivityHotList = new ArrayList<>();
        mActivityMineList = new ArrayList<>();
        mActivityHistoryList = new ArrayList<>();
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    JSONObject jsonObject = JsonUtil.getJsonObjectFromMap(response.getReturnContent());
                    try {
                        boolean flag = false;
                        JSONArray jsonArrayHot = jsonObject.getJSONArray("beginningActivityList");
                        if (jsonArrayHot.length() != 0) {
                            for (int i = 0; i < jsonArrayHot.length(); i++) {
                                BossActivityBean storeBean = JsonUtil.getGson().fromJson(jsonArrayHot.getString(i), BossActivityBean.class);
                                mActivityHotList.add(storeBean);
                            }
                            flag = true;
                        }
                        bindActivityHot();
                        JSONArray jsonArrayMine = jsonObject.getJSONArray("joinedActivityList");
                        if (jsonArrayMine.length() != 0) {
                            for (int i = 0; i < jsonArrayMine.length(); i++) {
                                BossActivityBean storeBean = JsonUtil.getGson().fromJson(jsonArrayMine.getString(i), BossActivityBean.class);
                                mActivityMineList.add(storeBean);
                            }
                            flag = true;
                        }
                        bindActivityMine();
                        JSONArray jsonArray = jsonObject.getJSONArray("historyActivityList");
                        if (jsonArray.length() != 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                BossActivityBean storeBean = JsonUtil.getGson().fromJson(jsonArray.getString(i), BossActivityBean.class);
                                mActivityHistoryList.add(storeBean);
                            }
                            flag = true;
                        }
                        bindActivityHistory();

                        showEmpty(flag);
                        refreshLayout.finishRefresh();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        ApiMethodBoss.getActivityList(new SimpleLbObserver(listener, otherListener),this, FragmentEvent.DESTROY_VIEW);
    }

    private void bindActivityHot() {
        if (mActivityHotList.size() != 0) {
            layoutWholeHot.setVisibility(View.VISIBLE);
            layoutActivityHot.removeAllViews();
            for (int i = 0; i < mActivityHotList.size(); i++) {
                LbActivityItem lbActivityItem = new LbActivityItem(mContext);
                lbActivityItem.setBossActivityBean(mActivityHotList.get(i), LbActivityItem.ACTIVITY_REC);
                lbActivityItem.enableClick();
                layoutActivityHot.addView(lbActivityItem);
            }
        } else {
            layoutWholeHot.setVisibility(View.GONE);
        }
    }

    private void bindActivityMine() {
        if (mActivityMineList.size() != 0) {
            layoutWholeMine.setVisibility(View.VISIBLE);
            layoutActivityMine.removeAllViews();
            for (int i = 0; i < mActivityMineList.size(); i++) {
                LbActivityItem lbActivityItem = new LbActivityItem(mContext);
                lbActivityItem.setBossActivityBean(mActivityMineList.get(i), LbActivityItem.ACTIVITY_MINE);
                lbActivityItem.enableClick();
                layoutActivityMine.addView(lbActivityItem);
            }
        } else {
            layoutWholeMine.setVisibility(View.GONE);
        }
    }

    private void bindActivityHistory() {
        if (mActivityHistoryList.size() != 0) {
            layoutWholeHistory.setVisibility(View.VISIBLE);
            layoutActivityHistory.removeAllViews();
            for (int i = 0; i < mActivityHistoryList.size(); i++) {
                LbActivityItem lbActivityItem = new LbActivityItem(mContext);
                lbActivityItem.setBossActivityBean(mActivityHistoryList.get(i), LbActivityItem.ACTIVITY_HIS);
                lbActivityItem.enableClick();
                layoutActivityHistory.addView(lbActivityItem);
            }
        } else {
            layoutActivityHistory.setVisibility(View.GONE);
        }
    }

    private void showEmpty(boolean flag) {
        if (!flag) {
            layoutEmptyView.setVisibility(View.VISIBLE);
        } else {
            layoutEmptyView.setVisibility(View.GONE);
        }
    }

}
